package com.example.tliaswebmanagement.AOP;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect//声明当前类为切面类
@Slf4j
public class RecordTimeAspect {
    @Pointcut("execution(* com.example.tliaswebmanagement.service.impl.*.*(..)))")//可用于抽取公共切入点
    public void pt(){}

        //@Before("execution(* com.example.tliaswebmanagement.service.impl.*.*(..)))")

    //前置通知
    @Before("pt()")//引入切点
        public void recordTime(JoinPoint joinPoint){
            long startTime = System.currentTimeMillis();
            log.info("方法执行前，记录开始时间：{}",startTime);
        }



        //环绕通知
        @Around("execution(* com.example.tliaswebmanagement.service.impl.*.*(..))")
        public Object recordTime3(ProceedingJoinPoint pjp) throws Throwable {
            long startTime = System.currentTimeMillis();
            Object result = pjp.proceed();
            long endTime = System.currentTimeMillis();
            long time = endTime - startTime;
            log.info("方法执行时间{}",time);
            return result;
        }


    //后置通知
    @After("pt()")
    public void after(JoinPoint joinPoint){
        log.info("after");
    }
    //返回后通知
    @AfterReturning("execution(* com.example.tliaswebmanagement.service.impl.*.*(..)))")
    public void recordTime2(JoinPoint joinPoint){
        long endTime = System.currentTimeMillis();
        log.info("方法执行后，记录结束时间：{}",endTime);
    }

    //异常通知（程序在出现异常的情况下，执行的后置通知）
    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint joinPoint){
        log.info("afterThrowing ...");
    }
}
